package com.ztc.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @类名：RichHtmlHandler.java
 * @作者：李晓平
 * @时间：2018年6月28日 下午4:36:41
 * @描述：富文本Html处理器，主要处理图片及编码
 */
public class RichHtmlHandler {

	private Document doc = null;
	private String html;

	private String docSrcParent = "";
	private String docSrcLocationPrex = "";
	private String nextPartId;
	private String shapeidPrex;
	private String spidPrex;
	private String typeid;

	private String handledDocBodyBlock;
	private List<String> docBase64BlockResults = new ArrayList<String>();
	private List<String> xmlImgRefs = new ArrayList<String>();
	
	private String[] cssStyle = new String[]{"color", "font-family", "font-size", "background", "background-color"};

	public RichHtmlHandler(String html) {
		this.html = html;
		doc = Jsoup.parse(wrappHtml(this.html));
	}

	public void re_init(String html) {
		doc = null;
		doc = Jsoup.parse(wrappHtml(html));
		docBase64BlockResults.clear();
		xmlImgRefs.clear();
	}

	/**
	 * @Description: 获得已经处理过的HTML文件
	 * @param @return
	 * @return String
	 * @throws IOException
	 * @throws @author:LiaoFei
	 * @date:2016-3-28 下午4:16:34
	 */
	public void handledHtml(boolean isWebApplication) throws Exception {
		Elements elements = doc.getElementsByTag("p");
		System.out.println("doc:\n" + doc);
		if(elements != null && elements.size() > 0) {
			for (Element item : elements) {
				String style = item.attr("style"); // 样式
				String newStyle = handledStyle(style);
				item.attr("style", newStyle);
			}
		}
		
		Elements imags = doc.getElementsByTag("img");
		System.out.println("new-doc:\n" + doc);
		if (imags == null || imags.size() == 0) {
			// 返回编码后字符串
			return;
			// handledDocBodyBlock = WordHtmlGeneratorHelper.string2Ascii(html);
		}

		// 转换成word mht 能识别图片标签内容，去替换html中的图片标签

		for (Element item : imags) {
			// 把文件取出来
			String src = item.attr("src");
			String srcRealPath = src;

			// String thepaths =
			// RichHtmlHandler.class.getClassLoader().getResource("").toString();
			// System.out.println("src="+src+" thepaths="+thepaths);
			if (isWebApplication) {
				// String
				// contentPath=RequestResponseContext.getRequest().getContextPath();
				// if(!StringUtils.isEmpty(contentPath)){
				// if(src.startsWith(contentPath)){
				// src=src.substring(contentPath.length());
				// }
				// }
				//
				// srcRealPath =
				// RequestResponseContext.getRequest().getSession()
				// .getServletContext().getRealPath(src);

			}

//			File imageFile = new File(srcRealPath);
//			String imageFielShortName = imageFile.getName();
			String imageFielShortName = srcRealPath.substring(srcRealPath.lastIndexOf(File.separator) + 1, srcRealPath.indexOf("."));
			String fileTypeName = WordImageConvertor.getFileSuffix(srcRealPath);

			String docFileName = "image" + UUID.randomUUID().toString() + "." + fileTypeName;
			String srcLocationShortName = docSrcParent + "/" + docFileName;

			String styleAttr = item.attr("style"); // 样式
			// 高度
			String imagHeightStr = item.attr("height");
			if (StringUtils.isEmpty(imagHeightStr)) {
				imagHeightStr = getStyleAttrValue(styleAttr, "height");
			}
			// 宽度
			String imagWidthStr = item.attr("width");
			if (StringUtils.isEmpty(imagHeightStr)) {
				imagHeightStr = getStyleAttrValue(styleAttr, "width");
			}

			imagHeightStr = imagHeightStr.replace("px", "");
			imagWidthStr = imagWidthStr.replace("px", "");
			if (StringUtils.isEmpty(imagHeightStr)) {
				// 去得到默认的文件高度
				imagHeightStr = "0";
			}
			if (StringUtils.isEmpty(imagWidthStr)) {
				imagWidthStr = "0";
			}
			int imageHeight = Integer.parseInt(imagHeightStr);
			int imageWidth = Integer.parseInt(imagWidthStr);

			// 得到文件的word mht的body块
			String handledDocBodyBlock = WordImageConvertor.toDocBodyBlock(srcRealPath,
					imageFielShortName, imageHeight, imageWidth, styleAttr, srcLocationShortName,
					shapeidPrex, spidPrex, typeid);

			// 这里的顺序有点问题：应该是替换item，而不是整个后面追加
			// doc.rreplaceAll(item.toString(), handledDocBodyBlock);
			item.after(handledDocBodyBlock);
			// item.parent().append(handledDocBodyBlock);
			item.remove();
			// 去替换原生的html中的imag

			String base64Content = WordImageConvertor.imageToBase64(srcRealPath);
			String contextLoacation = docSrcLocationPrex + "/" + docSrcParent + "/" + docFileName;

			String docBase64BlockResult = WordImageConvertor.generateImageBase64Block(nextPartId,
					contextLoacation, fileTypeName, base64Content);
			docBase64BlockResults.add(docBase64BlockResult);

			String imagXMLHref = "<o:File HRef=3D\"" + docFileName + "\"/>";
			xmlImgRefs.add(imagXMLHref);

		}

	}

	private String getStyleAttrValue(String style, String attributeKey) {
		if (StringUtils.isEmpty(style)) {
			return "";
		}

		// 以";"分割
		String[] styleAttrValues = style.split(";");
		for (String item : styleAttrValues) {
			// 在以 ":"分割
			String[] keyValuePairs = item.split(":");
			if (attributeKey.equals(keyValuePairs[0].trim())) {
				return keyValuePairs[1];
			}
		}

		return "";
	}
	
	private String handledStyle(String style) {
		if (StringUtils.isNotEmpty(style)) {
			// 以";"分割
			String[] styleAttrValues = style.split(";");
			for (String item : styleAttrValues) {
				// 在以 ":"分割
				String[] keyValuePairs = item.split(":");
				for(String one : cssStyle) {
					if(keyValuePairs[0].trim().equals(one)) {
						style = style.replace(item, "");
					}
				}
			}
		}
		style += ";color:black;font-family:SimSun;font-size:12px;";
		return style;
	}

	private String wrappHtml(String html) {
		// 因为传递过来都是不完整的doc
		StringBuilder sb = new StringBuilder();
		sb.append("<html>");
		sb.append("<body>");
		sb.append("<style>table {border-collapse: collapse;font-size:10.5pt;border: 1px solid #000;width: 100%;} table td,th {border: 1px solid #000;line-height:20px;}</style>");
		sb.append(html);
		sb.append("</body>");
		sb.append("</html>");
		return sb.toString();
	}

	public String getDocSrcLocationPrex() {
		return docSrcLocationPrex;
	}

	public void setDocSrcLocationPrex(String docSrcLocationPrex) {
		this.docSrcLocationPrex = docSrcLocationPrex;
	}

	public String getNextPartId() {
		return nextPartId;
	}

	public void setNextPartId(String nextPartId) {
		this.nextPartId = nextPartId;
	}

	public String getHandledDocBodyBlock() {
		String raw = WordHtmlGeneratorHelper.string2Ascii(doc.getElementsByTag("body").html());
		return raw.replace("=3D", "=").replace("=", "=3D");
	}

	public String getRawHandledDocBodyBlock() {
		String raw = doc.getElementsByTag("body").html();
		return raw.replace("=3D", "=").replace("=", "=3D");
	}

	public List<String> getDocBase64BlockResults() {
		return docBase64BlockResults;
	}

	public List<String> getXmlImgRefs() {
		return xmlImgRefs;
	}

	public String getShapeidPrex() {
		return shapeidPrex;
	}

	public void setShapeidPrex(String shapeidPrex) {
		this.shapeidPrex = shapeidPrex;
	}

	public String getSpidPrex() {
		return spidPrex;
	}

	public void setSpidPrex(String spidPrex) {
		this.spidPrex = spidPrex;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getDocSrcParent() {
		return docSrcParent;
	}

	public void setDocSrcParent(String docSrcParent) {
		this.docSrcParent = docSrcParent;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}
}
